package com.ddh.bankmanager.common;

import com.ddh.bankmanager.enum_.ResultCode;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class Result<T> {
    private Integer code;
    private String message;
    private T data;
    private Map<String, Object> extra = new HashMap<>();

    // 私有构造函数防止外部实例化
    private Result() {}

    // 成功的结果
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    // 成功的结果，不带数据
    public static <T> Result<T> success() {
        return success(null);
    }

    // 失败的结果
    public static <T> Result<T> failed(ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    // 失败的结果
    public static <T> Result<T> failed(Integer code,String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    // 自定义错误信息的失败结果
    public static <T> Result<T> failed(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.FAIL.getCode());
        result.setMessage(message);
        return result;
    }

    // 添加额外信息
    public Result<T> addExtra(String key, Object value) {
        if (key != null && value != null) {
            this.extra.put(key, value);
        }
        return this;
    }
}